// WebServer.h: interface for the CWebServer class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_WEBSERVER_H__FD08D2CA_1BDA_4300_A4B3_31B9E6B41488__INCLUDED_)
#define AFX_WEBSERVER_H__FD08D2CA_1BDA_4300_A4B3_31B9E6B41488__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "WebFile.h"

class CWebServer : public CSocket
{
public:
	CWebServer();
	virtual ~CWebServer();

	bool Initialize( UINT nPort = 0 );

	UINT GetPort() const;

	bool AddWebFile( const CWebFile* pFile );
	bool AddWebFileAlias( const CWebFile* pFile, LPCTSTR szAlias );
	bool RemoveWebFile( const CWebFile* pFile );

protected:
	enum ResponseStatus
	{
		rsOK,
		rsFileNotFound,
		rsBadRequest,
		rsInternalServerError
	};

	static void WriteCRLF( CSocket& request )
	{
		request.Send( "\r\n", 2 );
	}

	static void WriteResponseStatus( CSocket& request, ResponseStatus eStatus );
	static void WriteResponseStatus( CSocket& request, LPCSTR szStatus );

	static void WriteResponseHeader( CSocket& request, LPCSTR szContentType, UINT nContentSize = 0 );

	static void WriteFileContents( CSocket& request, LPCSTR szContentType, void* pContents, UINT nContentSize );

private:
	virtual void OnAccept( int nErrorCode );
	void ProcessRequest( CSocket& request ) const;
	void GetFile( CSocket& request, LPCSTR szFileName ) const;

private:
	typedef CMap< CString, LPCTSTR, const CWebFile*, const CWebFile* > CWebFileMap;

	CWebFileMap m_mapWebFiles;
	UINT m_nPort;
};

#endif // !defined(AFX_WEBSERVER_H__FD08D2CA_1BDA_4300_A4B3_31B9E6B41488__INCLUDED_)
